package org.birrap.ejb;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.birrap.entities.RolesT;


@Stateless
public class RolesTBean implements RolesTBeanLocal {

    @PersistenceContext(unitName = "BirrApp-ejbPU")
    private EntityManager em;
    
    @Override
    public List<RolesT> findAll() {
         Query query = em.createNamedQuery("RolesT.findAll");
       
       return (List<RolesT>)query.getResultList();
    }

    @Override
    public RolesT findByIdRol(Integer id) {
      RolesT res = null;
      Query query = em.createNamedQuery("RolesT.findByIdRol").setParameter("id", id);
      
      try{
          res = (RolesT)query.getSingleResult();
      }
      catch (NoResultException e){
          System.out.println("No existe el id");
      }
      return res;
    }

    @Override
    public List<RolesT> findByDescripcion(String descripcion) {
      List<RolesT> res = null;
      Query query = em.createNamedQuery("RolesT.findByDescripcion")
              .setParameter("descripcion","%" + descripcion+ "%");
      
      try{
          res = (List<RolesT>)query.getResultList();
      }
      catch (NoResultException e){
          System.out.println("No existe la descripcion");
      }
      return res;
    
    }

    @Override
    public RolesT insert(RolesT rol) {
        return update(rol);
    }

    @Override
    public RolesT update(RolesT rol) {
        return em.merge(rol);
    }
    


}
